//
//  SQMyViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/6.
//

#import "SQMyViewController.h"

#import "SQMyModel.h"

#import "AppDelegate.h"

#import "SQUserInformationViewController.h"
#import "SQChangePasswordViewController.h"
#import "SQMessageViewController.h"
#import "SQNewViewController.h"

#import "SQNewModel.h"

@interface SQMyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *lab_name;
@property (weak, nonatomic) IBOutlet UILabel *lab_company;
@property (weak, nonatomic) IBOutlet UIImageView *imgV_pic;
@property (weak, nonatomic) IBOutlet UILabel *lab_department;
@property (weak, nonatomic) IBOutlet UILabel *lab_jobs;
@property (strong, nonatomic) SQMyModel *myModel;


@end

@implementation SQMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidNav = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getData) name:@"userInformation" object:nil];

    [self getData];
}

-(void)getData{
    [SQMyModel getDataWithParams:@{@"m_id":UID} WithCallBack:^(SQMyModel * data) {
        self.myModel = data;
        self.lab_name.text = data.data.m_name;
        self.lab_company.text = data.data.m_enterprise_text;
        self.lab_department.text = [self join:@[[self isEmptyNull:data.data.m_one_text],[self isEmptyNull:data.data.m_two_text],[self isEmptyNull:data.data.m_three_text],[self isEmptyNull:data.data.m_four_text]]];
        self.lab_jobs.text = data.data.m_title;
        
        [self.imgV_pic setImageWithURL:[NSURL URLWithString:data.data.m_pic] placeholder:[UIImage imageNamed:@"header"]];
    }];
}

- (NSString *)join:(NSArray<NSString *> *)strs{
    NSString *str = @"";
    for (NSString *key in strs) {
        if (key.length > 0) {
            str = [str stringByAppendingFormat:@"%@/",key];
        }
    }
    if (!str.length)return @"";
    return [str substringToIndex:str.length - 1];
}

- (NSString *)isEmptyNull:(NSString *)string{
    if (string == nil || string == NULL) {
        string = @"";
    }
    return string;
}


//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

/**消息*/
- (IBAction)clickMessage:(UIButton *)sender {
    SQMessageViewController * messageVC = SQMessageViewController.new;
    [self pushVC:messageVC];
}

/**用户信息*/
- (IBAction)clickUseinfo:(UIButton *)sender {
    SQUserInformationViewController * userInformationVC = SQUserInformationViewController.new;
    [self pushVC:userInformationVC];
}

/**修改密码*/
- (IBAction)clickPassword:(UIButton *)sender {
    SQChangePasswordViewController * changePasswordVC = SQChangePasswordViewController.new;
    changePasswordVC.tel = self.myModel.data.m_phone;
    [self pushVC:changePasswordVC];
}

/**帮助中心*/
- (IBAction)clickHelp:(UIButton *)sender {
}

/**帮助新闻公告*/
- (IBAction)clickNew:(UIButton *)sender {
    SQNewViewController * newVC = SQNewViewController.new;
    [self pushVC:newVC];
  
    
}

/**退出登录*/
- (IBAction)clickExit:(UIButton *)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否退出登录" message:@"" preferredStyle:UIAlertControllerStyleAlert];

       

       [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

           NSLog(@"点击取消");

       }]];

       

      [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MID"];
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MName"];
          
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MLevel"];
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MEnterpriseId"];
          
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MEnterpriseText"];
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MGroup"];
          
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MNumber"];
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MOne"];
          
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MOneText"];
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MTwo"];
          
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MThree"];
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MFour"];
          
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MCode"];
        
          
          [[AppDelegate getAppDelegate] jump];
          

       }]];


     
      [self presentViewController:alertController animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
